package com.example.gokchinesefoodmappcdev.util;


import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Date;
import java.util.Map;

@Component
public class JWTUtil {
    /*
     * 定义密钥
     * */

    @Value("${jwt.config.secret}")
    private String secret;

    @Value("${jwt.config.tokenExpiredSeconds}")
    private int tokenExpiredSeconds;

    public String createToken(Map<String,Object> payLoadInfo,int tokenExpiredSeconds){
        //数值、字符串、布尔值，日期，数值（String,long,Integer）



            JWTCreator.Builder builder = JWT.create();
        //存入简单的用户信息
        for (Map.Entry<String, Object> entry : payLoadInfo.entrySet()) {
            setPayLoadInfo(builder,entry.getKey(),entry.getValue());

        }

        //设置默认过期时间
        Calendar instance = Calendar.getInstance();
        instance.add(Calendar.SECOND,tokenExpiredSeconds);
        builder.withExpiresAt(instance.getTime());

        //设置加密方式，生成token,返回token
        return builder.sign(Algorithm.HMAC256(secret));

    }

    public String createToken(Map<String, Object> payLoadInfo){
        return createToken(payLoadInfo,1800);
    }

    /*
    * 对token进行校验方法，如果没有抛出异常，反之，则证明验证通过
    * */
    public void verify(String token){
        JWTVerifier jwtVerifier = JWT.require(Algorithm.HMAC256(secret)).build();
        jwtVerifier.verify(token);
    }

    /*
    * 从解析后的token获取数据
    * token:jwtToken
    * */
    public<T> T getValue(String token,String key,Class<T> alass){
        DecodedJWT decodedJWT = JWT.require(Algorithm.HMAC256(secret)).build().verify(token);
        return decodedJWT.getClaim(key).as(alass);
    }

    public<T> T[] getArrayValue(String token,String key,Class<T> alass){
        DecodedJWT decodedJWT = JWT.require(Algorithm.HMAC256(secret)).build().verify(token);
        return decodedJWT.getClaim(key).asArray(alass);
    }

    private static void setPayLoadInfo(JWTCreator.Builder builder, String key,Object value){

        Class<?> aClass = value.getClass();
        if(aClass.equals(String.class)){
            builder.withClaim(key,(String) value);
        }
        else if(aClass.equals(Boolean.class)){
            builder.withClaim(key,(Boolean) value);
        }
        else if(aClass.equals(Date.class)){
            builder.withClaim(key,(Date) value);
        }
        else if(aClass.equals(Long.class)){
            builder.withClaim(key,(Long) value);
        }
        else if(aClass.equals(Integer.class)){
            builder.withClaim(key,(Integer) value);
        }
        else if(aClass.equals(Double.class)){
            builder.withClaim(key,(Double) value);
        }

        else if(aClass.equals(String[].class)){
            builder.withArrayClaim(key,(String[]) value);
        }
        else if(aClass.equals(Long[].class)){
            builder.withArrayClaim(key,(Long[]) value);
        }
        else if(aClass.equals(Integer[].class)){
            builder.withArrayClaim(key,(Integer[]) value);
        }else {
            throw new IllegalArgumentException("存入token中的数据不合法！存入值的类型一个为：String、Boolean、Date、Long、Integer、Double、" +
                    "、String[]、Long[]、Integer[]之一,但是你传入的是："+aClass);
        }
    }


}
